import requests  


class GrafanaToken:
    def __init__(self, grafana_host_url, username, password) -> None:
        self.grafana_host_url = grafana_host_url
        self.username = username
        self.password = password

    def response_print(self, response):
        # 检查请求是否成功  
        if response.status_code == 200 or response.status_code == 201:  
            print('请求成功:', response.json())  
        else:  
            print(f'请求失败: {response.status_code} - {response.text}')
        
        return True

    def create_organization(self, organization_name):
        """
            创建组织
        """
        data = {  
            'name': organization_name  
        }  

        grafana_sub_url = "/api/orgs"

        grafana_url = self.grafana_host_url + grafana_sub_url

        # 使用requests库发送POST请求  
        response = requests.post(grafana_url, json=data, auth=(self.username, self.password))  

        # 检查请求是否成功  
        # 请求成功: {'message': 'Organization created', 'orgId': 2}
        self.response_print(response)
        
        return True
 
    def organization_changed(self, orgId):
        """
            用户切换到组织
        """

        grafana_sub_url = "/api/user/using/"

        grafana_url = self.grafana_host_url + grafana_sub_url + orgId

        response = requests.post(grafana_url, auth=(self.username, self.password))  

        self.response_print(response)
        # 请求成功: {'message': 'Active organization changed'}
        
        return True      
    
    def create_serviceaccounts(self, server_username, server_user_role="Admin"):
        """
            创建服务账号
        """

        data = {  
            'name': server_username,
            'role': server_user_role  
        }  

        grafana_sub_url = "/api/serviceaccounts"

        grafana_url = self.grafana_host_url + grafana_sub_url

        response = requests.post(grafana_url, json=data, auth=(self.username, self.password))

        self.response_print(response)
        # 请求成功: {'id': 2, 'name': 'sumz', 'login': 'sa-2-sumz', 'orgId': 2, 'isDisabled': False, 'role': 'Viewer', 'tokens': 0, 'avatarUrl': ''}
        
        return True
    
    def alter_serviceaccounts(self, server_user_id, server_username, server_user_role):
        """
            修改服务账号
        """

        data = {  
            'name': server_username,
            'role': server_user_role  
        }  

        grafana_sub_url = "/api/serviceaccounts/" + server_user_id

        grafana_url = self.grafana_host_url + grafana_sub_url

        response = requests.patch(grafana_url, json=data, auth=(self.username, self.password))

        self.response_print(response)
        # 请求成功: {'id': 2, 'message': 'Service account updated', 'name': 'sumz', 'serviceaccount': {'id': 2, 'name': 'sumz', 'login': 'sa-2-sumz', 'orgId': 2, 'isDisabled': False, 'createdAt': '2024-08-07T15:35:57+08:00', 'updatedAt': '2024-08-07T15:35:57+08:00', 'avatarUrl': '/avatar/96f43ca7e8626876f64f7ac465b53320', 'role': 'Admin', 'teams': None}}
        
        return True

    def create_serviceaccounts_token(self, service_account_id, token_name):
        """
            创建服务账户令牌
        """

        data = {  
            'name': token_name  
        }  

        grafana_url = self.grafana_host_url + f"/api/serviceaccounts/{service_account_id}/tokens"

        response = requests.post(grafana_url, json=data, auth=(self.username, self.password))

        self.response_print(response)
        # 请求成功: {'id': 1, 'name': 'sumz', 'key': 'glsa_QrkmPBYbmGQsodxttVIqw8TtnDZQwJxt_2677af6c'}
        
        return True